package com.example.jpa1.service;


import com.example.jpa1.dao.UserDao;
import com.example.jpa1.model.UserModel;
import com.example.jpa1.pojo.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
public class UserService {

    @Autowired
    private UserDao userDao;


    public UserModel saveUser(UserModel user){
        User user1 = new User();
//        user1.setName(user.getName());
//        user1.setAge(user.getAge());
        BeanUtils.copyProperties(user,user1);
        userDao.save(user1);

        user1.setName(user.getName()+"edit");

//        throw new RuntimeException("模拟插入错误");
        return null;
    }

    public List<UserModel> list(){
        List<UserModel> list = new ArrayList<>();
        List<User> users = userDao.list();
        for(User user:users){
            UserModel userModel = new UserModel();
            BeanUtils.copyProperties(user,userModel);
            list.add(userModel);
        }
        return list;
    }
}
